from flask import current_app
from qiniu import Auth, put_data

# 需要填写你的 Access Key 和 Secret Key

access_key = 'eAkegbJIXpNPd_4jI41xx_smJyqHdX5BcZ6E_CC5'
secret_key = '_OqALH_eFSJIeu_eAFo8NFZR6yTOkldpe-dcoUGv'


def storage(data):
    try:
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
    except Exception as e:
        current_app.logger.error(e)
        raise e

    # 要上传的空间
    bucket_name = 'storage1'

    # 生成上传 Token, 可以指定过期时间等
    # 文件名可以不传，服务器可以帮我们自动生成
    token = q.upload_token(bucket_name, None, 3600)

    ret, info = put_data(token, None, data)
    if info.status_code != 200:
        raise Exception('上传图片失败')
    # print(info)
    # print(ret)
    return ret['key']


if __name__ == '__main__':
    file = input('请输入文件路径')
    with open(file, 'rb') as f:
        storage(f.read())
